package cn.janus.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.janus.component.RocketMQProducer;
import cn.janus.pojo.dto.BillInfoDTO;
import cn.janus.pojo.entity.Bill;
import cn.janus.mapper.BillMapper;
import cn.janus.pojo.vo.BillInfoVO;
import cn.janus.service.IBillService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author janus
 * @since 2024-06-15
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {
    @Autowired
    private RedisTemplate<String, Bill> redisTemplate;

    @Autowired
    private RocketMQProducer rocketMQProducer;


    @Override
    public BillInfoVO billInfoService(BillInfoDTO billInfoDTO) {
        ListOperations<String, Bill> listOperations = redisTemplate.opsForList();
        List<Bill> bills = listOperations.range("bills", 0, billInfoDTO.getCurrentPage() * 10 - 1);
        if (!ObjectUtil.isNull(bills)) {
            Long size = listOperations.size("bills");
            return new BillInfoVO(bills, size % 10 == 0 ? size : size % 10);
        } else {
            Page<Bill> billPage = this.page(Page.of(billInfoDTO.getCurrentPage(), 10),
                    new LambdaQueryWrapper<Bill>().orderByDesc(Bill::getTime));
            return new BillInfoVO(billPage.getRecords(),
                    billPage.getTotal() % 10 == 0 ? billPage.getTotal() / 10 : billPage.getTotal() / 10 + 1);
        }

    }

    @Override
    public void UpdateBillById(Bill bill) {
        Bill originBill = bill;
        ListOperations<String, Bill> listOperations = redisTemplate.opsForList();
        Long size = listOperations.size("bills");
        List<Bill> bills;
        if (size != null) {
            bills = listOperations.range("bills", 0, size);
            for (int i = 0; i < size; i++) {
                if (bills != null && bills.get(i).getId().equals(bill.getId())) {
                    Bill tempBill = bills.get(i);
                    bill.setAmount(bill.getAmount() != null ? bill.getAmount() : tempBill.getAmount());
                    bill.setTime(bill.getTime() != null ? bill.getTime() : tempBill.getTime());
                    bill.setCategory(bill.getCategory() != null ? bill.getCategory() : tempBill.getCategory());
                    bill.setPurpose(bill.getPurpose() != null ? bill.getPurpose() : tempBill.getPurpose());
                    bill.setRemark(bill.getRemark() != null ? bill.getRemark() : tempBill.getRemark());
                    listOperations.set("bills", i, bill);
                }
            }
            rocketMQProducer.sendMessage("your-topic:testTag", originBill);
//            rocketMQProducer.sendMessageWithTag("your-topic", "test_tag", "tally redis sync");
        }
    }
}

